<template>
  <div class="bookings-container">
    <div class="page-header">
      <h1>预约管理</h1>
      <p>管理您的球场预约记录</p>
    </div>
    
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="3" animated />
    </div>
    
    <div v-else-if="userBookings.length === 0" class="empty-state">
      <el-empty description="暂无预约记录">
        <el-button type="primary" @click="$router.push('/')">
          去预约球场
        </el-button>
      </el-empty>
    </div>
    
    <div v-else class="bookings-list">
      <el-card
        v-for="booking in userBookings"
        :key="booking.id"
        class="booking-card"
        shadow="hover"
      >
        <template #header>
          <div class="booking-header">
            <div class="booking-info">
              <h3>{{ booking.courts.name }}</h3>
              <el-tag :type="getStatusTag(booking.status)">
                {{ getStatusText(booking.status) }}
              </el-tag>
            </div>
            <div class="booking-actions">
              <el-button
                v-if="booking.status === 'confirmed'"
                type="danger"
                size="small"
                :icon="Delete"
                :loading="cancellingBooking === booking.id"
                @click="handleCancelBooking(booking)"
              >
                取消预约
              </el-button>
            </div>
          </div>
        </template>
        
        <div class="booking-content">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="预约日期">
              {{ booking.booking_date }}
            </el-descriptions-item>
            <el-descriptions-item label="时间段">
              {{ booking.time_slots.time_range }}
            </el-descriptions-item>
            <el-descriptions-item label="价格">
              ¥{{ booking.time_slots.price }}
            </el-descriptions-item>
            <el-descriptions-item label="预约时间">
              {{ formatBookingTime(booking.created_at) }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Delete } from '@element-plus/icons-vue'
import { useCourtStore } from '@/stores/court'
import dayjs from 'dayjs'

const courtStore = useCourtStore()

// 状态
const loading = ref(false)
const userBookings = ref([])
const cancellingBooking = ref(null)

// 方法
const getStatusTag = (status) => {
  const tagMap = {
    confirmed: 'success',
    cancelled: 'info',
    completed: 'primary'
  }
  return tagMap[status] || 'info'
}

const getStatusText = (status) => {
  const textMap = {
    confirmed: '已确认',
    cancelled: '已取消',
    completed: '已完成'
  }
  return textMap[status] || '未知'
}

const formatBookingTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}

const loadUserBookings = async () => {
  loading.value = true
  try {
    userBookings.value = await courtStore.getUserBookings()
  } catch (error) {
    console.error('加载用户预约失败:', error)
    ElMessage.error('加载预约失败')
  } finally {
    loading.value = false
  }
}

const handleCancelBooking = async (booking) => {
  try {
    await ElMessageBox.confirm(
      `确定要取消预约"${booking.courts.name}"的${booking.time_slots.time_range}时间段吗？`,
      '取消预约',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    cancellingBooking.value = booking.id
    
    const result = await courtStore.cancelBooking(booking.id)
    if (result.success) {
      ElMessage.success(result.message)
      // 重新加载预约列表
      await loadUserBookings()
    } else {
      ElMessage.error(result.message)
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('取消预约失败')
    }
  } finally {
    cancellingBooking.value = null
  }
}

onMounted(async () => {
  await loadUserBookings()
})
</script>

<style scoped>
.bookings-container {
  max-width: 800px;
  margin: 0 auto;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-header h1 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 2rem;
}

.page-header p {
  margin: 0;
  color: #666;
  font-size: 1.1rem;
}

.loading-container {
  padding: 20px;
}

.empty-state {
  text-align: center;
  padding: 60px 0;
}

.bookings-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.booking-card {
  transition: transform 0.3s ease;
}

.booking-card:hover {
  transform: translateY(-2px);
}

.booking-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.booking-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.booking-info h3 {
  margin: 0;
  color: #333;
}

.booking-content {
  margin-top: 15px;
}

@media (max-width: 768px) {
  .booking-header {
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
  }
  
  .booking-actions {
    width: 100%;
  }
  
  .booking-actions .el-button {
    width: 100%;
  }
}
</style>
